<!-- 会员编辑弹窗 -->
<template>
  <el-dialog
    width="1250px"
    :visible="visible"
    :lock-scroll="false"
    :destroy-on-close="true"
    custom-class="ele-dialog-form"
    :title="isUpdate?'修改房间':'添加房间'"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="132px">
      <el-form-item label="房间名称" prop="name">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.name"
          placeholder="房间名称"/>
      </el-form-item>

      <el-form-item label="门禁列表:">
        <el-select
          filterable
          clearable
          v-model="form.door_id"
          :options="doordaList"
          size="small"
          placeholder="-请选择服门禁-"
          class="ele-block">
          <el-option v-for="item in doordaList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>

      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          clearable
          :maxlength="20"
          v-model="form.sort"
          placeholder="排序"/>
      </el-form-item>

    </el-form>
    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import uploadImage from '@/components/uploadImage'
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
import TinymceEditor from '@/components/TinymceEditor';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

export default {
  name: 'MemberEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
    // 栏目数据
    cateList: Array
  },
  components: {uploadImage, TinymceEditor, Treeselect},
  data() {
    return {


      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 会员等级列表

      labelList: [],
      packageList: [],


      is_time: 1,

    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data, {});

        this.type = this.data.type;
        this.isUpdate = true;
      } else {
        this.form = {};
        this.form.is_time = 1;
        this.isUpdate = false;
      }
    }
  },


  mounted() {
    // 获取职级列表

    this.getDoordaList();


  },


  methods: {

    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 城市数据处理

          this.$http.post('/house/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    getDoordaList() {
      this.$http.get('/course/getDoordaList').then(res => {
        if (res.data.code === 0) {
          this.doordaList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },

  }
}
</script>

<style scoped>
</style>
